package com.shop.shop;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.FileFilter;
import java.util.ArrayList;

//代码自动生成器
public class Geenter {
    public static void main(String[] args) {
       //需要构建一个 代码自动生产器 对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略

        //1.全局配置
        GlobalConfig gc=new GlobalConfig();
        //获取系统的当前路径
        Object property = System.getProperty("user.dir");
        gc.setOutputDir(property+"/shop/src/main/java");
        gc.setAuthor("小🌤");  //作者注释
        gc.setOpen(false);//打开文件管理器
        gc.setFileOverride(false);//是否覆盖
        gc.setServiceName("%sService");//取掉后zhui
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
    //    gc.setSwagger2(true);
     mpg.setGlobalConfig(gc);

          //配置数据源
        DataSourceConfig ds = new DataSourceConfig();
           ds.setUrl("jdbc:mysql://127.0.0.1:3306/shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true");
           ds.setDriverName("com.mysql.cj.jdbc.Driver");
           ds.setUsername("root");
           ds.setPassword("root");
           ds.setDbType(DbType.MYSQL);
            mpg.setDataSource(ds);

            //包的配置
        PackageConfig pc=new PackageConfig();
        pc.setModuleName("shop");
        pc.setParent("com.shop");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        //"address","collect","commodity","coupon","image","introduction","merchant","order","sort","ur_cm",
        strategy.setInclude("shopringCart"); //设置要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel); //驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);  //自动Lombok

        //配置逻辑删除
        strategy.setLogicDeleteFieldName("deleted");
      //自动填充配置
        TableFill createTime = new TableFill("createTime", FieldFill.INSERT);
        TableFill updateTime = new TableFill("updateTime", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(createTime);list.add(updateTime);
        strategy.setTableFillList(list);

        //乐观锁配置
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        mpg.setStrategy(strategy);

        mpg.execute();//执行

    }
}
